package com.rj.bd.user.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.rj.bd.user.entity.User;
import com.rj.bd.user.service.IUserService;

/**
 * @DESC C层
 * @author 20565
 *
 */
@Controller
@RequestMapping(value={"/user"})
public class UserController {
	@Autowired
	public IUserService userService;
	HttpSession session;
/**
 * 查询
 * @param request
 * @param user
 * @return
 */
	@ResponseBody
	@RequestMapping("/query")
	public Map<String, Object> query(HttpServletRequest request,User user){
		Map<String, Object> map=new HashMap<String, Object>();
		List<User> list=userService.findAll(user);
		map.put("code", 200);
		map.put("msg", "查询成功");
		map.put("list", list);
		return map;
	}
	/**
	 * @DESC 添加
	 * @param request
	 * @param user
	 * @return
	 */
	
	@ResponseBody
	@RequestMapping("/add")
	public Map<String, Object> add(HttpServletRequest request,@RequestBody User user){
		System.out.println(user);
		Map<String, Object> map=new HashMap<String, Object>();
		userService.save(user);
		List<User> list=userService.findAll(user);
		map.put("list", list);
		map.put("code", 200);
		map.put("msg", "添加成功");
		return map;
	}
	
	/**
	 * @DESC 修改
	 * @param request
	 * @param user
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/editpage")
	public Map<String, Object> editpage(HttpServletRequest request,@RequestBody User user){
	Map<String, Object> map=new HashMap<String, Object>();
	String loginId = user.getLoginId();
	
	User userbyid = userService.seletById(loginId);
	
	map.put("user", userbyid);
	map.put("code", 200);
	map.put("msg", "查询成功");
	return map;
	}
	
	/**
	 * @DESC　删除
	 * @param request
	 * @param user
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delete")
	public Map<String, Object> delete(HttpServletRequest request,@RequestBody User user){
	Map<String, Object> map=new HashMap<String, Object>();
	userService.delete(user);
	List<User> list=userService.findAll(user);
	map.put("list", list);
	map.put("code", 200);
	map.put("msg", "删除成功");
	return map;
	}
	
	
	@ResponseBody
	@RequestMapping("/edit")
	public Map<String, Object> edit(HttpServletRequest request,@RequestBody User user){
		System.out.println(user);
	Map<String, Object> map=new HashMap<String, Object>();
	
	userService.edit(user);
	
	map.put("code", 200);
	map.put("msg", "修改成功");
	return map;
	}
}
